package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜品接口")
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    private void cache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        log.info("清空redis缓存的dish_,{}",keys);
        redisTemplate.delete(keys);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result insert(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        dishService.insert(dishDTO);

        cache("dish_" + dishDTO.getCategoryId());

        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result selectAll(DishPageQueryDTO dto){
        log.info("菜品分页查询");
        PageResult pageResult = dishService.selectAll(dto);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result select(@PathVariable Long id){
        log.info("根据id查询菜品");
        DishVO dishVO = dishService.select(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品");
        dishService.update(dishDTO);

        cache("dish_*");
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteAll(@RequestParam List<Long> ids){
        dishService.deleteAll(ids);

        cache("dish_*");

        return Result.success();
    }

    /**
     * 根据分类Id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类Id查询菜品")
    @GetMapping("/list")
    public Result selectCategory(Long categoryId){
        List<Dish> listDish = dishService.selectCategory(categoryId);
        return Result.success(listDish);
    }

    /**
     * 菜品起售,停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品起售,停售")
    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status , Long id){
        dishService.setStatus(status,id);

        cache("dish_*");

        return Result.success();
    }
}
